package cn.cmkj.auction.data.http;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by cunguoyao on 2018/4/9.
 */

public class Auction implements Serializable {
    private long id;
    private long goodsId;
    private String name;
    private String brief;
    private BigDecimal startPrice;
    private BigDecimal priceMarkup;
    private BigDecimal bond;//押金
    private String expiration_time;
    private int status;//
    private String statusStr;
    private long fromTime;
    private long toTime;
    private long lastModify;
    private long createTime;
    private BigDecimal nowPrice;
    private String bn;
    private String goodsName;
    private String imageUrl;
    private BigDecimal dealPrice;//成交价
    private String dealAccount;//成交者
    //以下是书画详情中 要用到的
    private List<ShuhuaImage> images;
    private List<ShuhuaProps> props;

    //和用户相关的属性
    private boolean isFav;
    private boolean isCart;
    private boolean isFrontMoney;

    public static Auction parseFromJsonForDetail(JSONObject jsonObj) {
        if(jsonObj == null)return null;
        JSONObject json = jsonObj.optJSONObject("page_product_basic");
        if(json == null)return null;
        Auction bean = new Auction();
        bean.setId(json.optLong("auction_id", 0));
        bean.setBn(json.optString("bn", ""));
        double bond = json.optDouble("bond", 0);
        bean.setBond(BigDecimal.valueOf(bond));
        bean.setBrief(json.optString("brief", ""));
        bean.setCreateTime(json.optLong("create_time", 0));
        bean.setExpiration_time(json.optString("expiration_time", ""));
        bean.setFromTime(json.optLong("from_time", 0));
        bean.setGoodsId(json.optLong("goods_id", 0));
        bean.setGoodsName(json.optString("goodname", ""));
        List<ShuhuaImage> images = ShuhuaImage.parseJsonArray(json.optJSONArray("image"), false);
        bean.setImages(images);
        bean.setProps(ShuhuaProps.parseJsonArray(json.optJSONArray("props")));
        bean.setLastModify(json.optLong("last_modify", 0));
        bean.setName(json.optString("name", ""));
        double nowPrice = json.optDouble("now_price", 0);
        bean.setNowPrice(BigDecimal.valueOf(nowPrice));
        double priceMarkup = json.optDouble("price_markup", 0);
        bean.setPriceMarkup(BigDecimal.valueOf(priceMarkup));
        double startPrice = json.optDouble("starting_price", 0);
        bean.setStartPrice(BigDecimal.valueOf(startPrice));
        bean.setStatusStr(json.optString("status", ""));
        bean.setStatus(json.optInt("int_status"));
        bean.setFav(jsonObj.optBoolean("is_fav"));
        bean.setCart(jsonObj.optBoolean("is_cart"));
        bean.setFrontMoney(jsonObj.optBoolean("is_frontMoney"));
        bean.setToTime(json.optLong("to_time", 0));
        JSONObject auctionMember = json.optJSONObject("auction_member");
        if(auctionMember != null) {
            double dealPrice = auctionMember.optDouble("now_price", 0);
            bean.setDealPrice(BigDecimal.valueOf(dealPrice));
            bean.setDealAccount(auctionMember.optString("login_account"));
        }
        return bean;
    }

    public static Auction parseFromJsonForList(JSONObject json) {
        if(json == null)return null;
        Auction bean = new Auction();
        bean.setId(json.optLong("auction_id", 0));
        bean.setBn(json.optString("bn", ""));
        double bond = json.optDouble("bond", 0);
        bean.setBond(BigDecimal.valueOf(bond));
        bean.setBrief(json.optString("brief", ""));
        bean.setCreateTime(json.optLong("create_time", 0));
        bean.setExpiration_time(json.optString("expiration_time", ""));
        bean.setFromTime(json.optLong("from_time", 0));
        bean.setGoodsId(json.optLong("goods_id", 0));
        bean.setGoodsName(json.optString("goodname", ""));
        List<ShuhuaImage> images = new ArrayList<>();
        images.add(ShuhuaImage.parseFromJson(json.optJSONObject("image"), false));
        if(images.size() > 0) {
            bean.setImageUrl(images.get(0).m_url);
        }
        bean.setImages(images);
        bean.setProps(ShuhuaProps.parseJsonArray(json.optJSONArray("props")));
        bean.setLastModify(json.optLong("last_modify", 0));
        bean.setName(json.optString("name", ""));
        double nowPrice = json.optDouble("now_price", 0);
        bean.setNowPrice(BigDecimal.valueOf(nowPrice));
        double priceMarkup = json.optDouble("price_markup", 0);
        bean.setPriceMarkup(BigDecimal.valueOf(priceMarkup));
        double startPrice = json.optDouble("starting_price", 0);
        bean.setStartPrice(BigDecimal.valueOf(startPrice));
        bean.setStatusStr(json.optString("status", ""));
        bean.setToTime(json.optLong("to_time", 0));
        return bean;
    }

    public static List<Auction> parseJsonArray(JSONArray array) {
        List<Auction> list = new ArrayList<>();
        if(array != null && array.length() > 0){
            for(int i=0;i<array.length();i++){
                list.add(parseFromJsonForList(array.optJSONObject(i)));
            }
        }
        return list;
    }

    public static Auction parseFromJsonPreOrder(JSONObject json) {
        if(json == null)return null;
        Auction bean = new Auction();
        bean.setId(json.optLong("auction_id", 0));
        bean.setBn(json.optString("bn", ""));
        double bond = json.optDouble("bond", 0);
        bean.setBond(BigDecimal.valueOf(bond));
        bean.setBrief(json.optString("brief", ""));
        bean.setCreateTime(json.optLong("create_time", 0));
        bean.setExpiration_time(json.optString("expiration_time", ""));
        bean.setFromTime(json.optLong("from_time", 0));
        bean.setGoodsId(json.optLong("goods_id", 0));
        bean.setGoodsName(json.optString("goodname", ""));
        List<ShuhuaImage> images = ShuhuaImage.parseJsonArray(json.optJSONArray("image"), false);
        bean.setImages(images);
        bean.setProps(ShuhuaProps.parseJsonArray(json.optJSONArray("props")));
        bean.setLastModify(json.optLong("last_modify", 0));
        bean.setName(json.optString("name", ""));
        double nowPrice = json.optDouble("now_price", 0);
        bean.setNowPrice(BigDecimal.valueOf(nowPrice));
        double priceMarkup = json.optDouble("price_markup", 0);
        bean.setPriceMarkup(BigDecimal.valueOf(priceMarkup));
        double startPrice = json.optDouble("starting_price", 0);
        bean.setStartPrice(BigDecimal.valueOf(startPrice));
        bean.setStatusStr(json.optString("status", ""));
        bean.setStatus(json.optInt("int_status"));
        bean.setToTime(json.optLong("to_time", 0));
        JSONObject auctionMember = json.optJSONObject("auction_member");
        if(auctionMember != null) {
            double dealPrice = auctionMember.optDouble("now_price", 0);
            bean.setDealPrice(BigDecimal.valueOf(dealPrice));
            bean.setDealAccount(auctionMember.optString("login_account"));
        }
        return bean;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public long getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(long goodsId) {
        this.goodsId = goodsId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getBrief() {
        return brief;
    }

    public void setBrief(String brief) {
        this.brief = brief;
    }

    public BigDecimal getStartPrice() {
        return startPrice;
    }

    public void setStartPrice(BigDecimal startPrice) {
        this.startPrice = startPrice;
    }

    public BigDecimal getPriceMarkup() {
        return priceMarkup;
    }

    public void setPriceMarkup(BigDecimal priceMarkup) {
        this.priceMarkup = priceMarkup;
    }

    public BigDecimal getBond() {
        return bond;
    }

    public void setBond(BigDecimal bond) {
        this.bond = bond;
    }

    public String getExpiration_time() {
        return expiration_time;
    }

    public void setExpiration_time(String expiration_time) {
        this.expiration_time = expiration_time;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getStatusStr() {
        return statusStr;
    }

    public void setStatusStr(String statusStr) {
        this.statusStr = statusStr;
    }

    public long getFromTime() {
        return fromTime;
    }

    public void setFromTime(long fromTime) {
        this.fromTime = fromTime;
    }

    public long getToTime() {
        return toTime;
    }

    public void setToTime(long toTime) {
        this.toTime = toTime;
    }

    public long getLastModify() {
        return lastModify;
    }

    public void setLastModify(long lastModify) {
        this.lastModify = lastModify;
    }

    public long getCreateTime() {
        return createTime;
    }

    public void setCreateTime(long createTime) {
        this.createTime = createTime;
    }

    public BigDecimal getNowPrice() {
        return nowPrice;
    }

    public void setNowPrice(BigDecimal nowPrice) {
        this.nowPrice = nowPrice;
    }

    public String getBn() {
        return bn;
    }

    public void setBn(String bn) {
        this.bn = bn;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public List<ShuhuaImage> getImages() {
        return images;
    }

    public void setImages(List<ShuhuaImage> images) {
        this.images = images;
    }

    public List<ShuhuaProps> getProps() {
        return props;
    }

    public void setProps(List<ShuhuaProps> props) {
        this.props = props;
    }

    public boolean isFav() {
        return isFav;
    }

    public void setFav(boolean fav) {
        isFav = fav;
    }

    public boolean isCart() {
        return isCart;
    }

    public void setCart(boolean cart) {
        isCart = cart;
    }

    public boolean isFrontMoney() {
        return isFrontMoney;
    }

    public void setFrontMoney(boolean frontMoney) {
        isFrontMoney = frontMoney;
    }

    public BigDecimal getDealPrice() {
        return dealPrice;
    }

    public void setDealPrice(BigDecimal dealPrice) {
        this.dealPrice = dealPrice;
    }

    public String getDealAccount() {
        return dealAccount;
    }

    public void setDealAccount(String dealAccount) {
        this.dealAccount = dealAccount;
    }
}
